#/usr/bin/python
import sys
from crypto import parseCipherFile, parseVectorFile, modInverse, writePlainFile, writeVectorFile, decrypt

from traceback import print_exception
#decrypt with the knapsack cipher		

try:
	#get file to encrypt
	input_fname = raw_input("Enter the name of the file to decrypt:  ")
	
	#get file into a string
	cipher = parseCipherFile(input_fname)
	
	#get super increasing set
	non_super_fname = raw_input("Enter the name of the file with the encryption set: ")
	
	#parse the file into a list of numbers
	non_super_set = parseVectorFile(non_super_fname)

	mod = int(raw_input("Enter the number to be used as the modulus:  "))
	
	mult = int(raw_input("Enter the number to be used as the multiplier:  "))
		
	#get the output file
	output_fname = raw_input("Enter the name of the file to output the plain text to:  ")
	
	inv = modInverse(mult,mod)
	super_set = [(i*inv) % mod for i in non_super_set]
	
	#decrypt file and write to output file
	plain = decrypt(cipher, super_set, mod, mult, inv)
	
	writePlainFile(output_fname, plain)		
	vector_fname = raw_input("Enter the name to output the super increasing set to:  ")
	
	writeVectorFile(vector_fname, super_set)
except KeyboardInterrupt:
	print()
	sys.exit()
except:
	print "You fail!! The code threw and exception."
	e = sys.exc_info()
	print_exception(e[0], e[1], e[2])
